﻿/*
 * Author:feitianxinhong
 * CreateDate:2014-10-21
 * CopyRight:[Tyingsoft] Twilight Software Development Studio ©2014
 * Description:Twilight2.1 for Bootstrap3.2 响应式布局
 */

/**
* @class Twi
* @singleton
* 
*/
(function ($) {
	var global = this;

	if (typeof Twi === 'undefined') {
		global.Twi = {};
	}
	Twi.global = global;

    /**
     * 空函数
     */
	Twi.emptyFn = function () { }

	/**
	* PageHandler路径
	*/
	Twi.Url = "/PageHandler.ashx";

	/**
	*config:$.ajax(config)的参数
	*config.data:参数配置
	*config.success:（做了些改进）  twiReturn.success == true时执行的函数
	*/
	Twi.Ajax = function (config) {
		var ajaxConfig = $.extend({
			url: Twi.Url,
			dataType: 'JSON',
			type: 'POST',
			loadingMask: false,          //提交时是否自动加载Twi.LoadingMask...
            maskMsg:'正在处理请稍后...',
			errorMsgTarget: 'body',  //错误信息显示的位置，默认：body 。 ID需带井号，如：#id
			emptyMsg: false           //显示错误信息前是否清空内容
		}, config);

        /*TwiLoadingMask*/
		var twiMask;
		if (ajaxConfig.loadingMask) {
		    twiMask = new Twi.LoadingMask({
		        msg: ajaxConfig.maskMsg
		    });
		    twiMask.show();		    
		}
		delete ajaxConfig.loadMask;
		delete ajaxConfig.maskMsg;

        /*ajax请求成功方法*/
		ajaxConfig.success = function (twiReturn, textStatus, jqXHR) {
			if (twiReturn.success) {
				if (typeof config.success == "function") config.success(twiReturn);
			}
			else {
				if (Twi.Msg) {
					Twi.Msg.Error(twiReturn.message);
				}
				else {
					if (ajaxConfig.emptyMsg) { $(ajaxConfig.errorMsgTarget).emptyMsg(); }
					$(ajaxConfig.errorMsgTarget).append('<div class="alert alert-warning alert-dismissable"><button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button><strong>温馨提示：</strong>' + twiReturn.errors + '</div>');
				}
				if (twiReturn.knowError == "102") {
					location.href = "/Login/Index";
				}
			}
		}

	    /*ajax请求最后执行的方法*/
		ajaxConfig.complete = function () {
		    if (typeof config.complete == "function") config.complete();
		    if (twiMask) { twiMask.hide(); }
		}

		$.ajax(ajaxConfig);
	}
	/**
	* Grid一页显示的记录条数
	*/
	Twi.PageSize = 15;

	/**
	*开发人员才能维护基础数据，发布的时候，或是给测试的时候，这个应设为false
	*/
	Twi.IsDeveloper = false;
	Twi.NotDeveloperError = "系统基础支撑数据，只能由开发人员维护，</br>否则系误操作可能会引起系统对应功能无法操作！";
	/**
	*获取地址栏参数
	*/
	Twi.GetUrlParam = function (name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) return unescape(r[2]);
		return null;
	}


	/**
	 *Cookies操作
	 */
	Twi.Cookies = {};
	Twi.Cookies.set = function (name, value) { var argv = arguments; var argc = arguments.length; var expires = (argc > 2) ? argv[2] : null; var path = (argc > 3) ? argv[3] : '/'; var domain = (argc > 4) ? argv[4] : null; var secure = (argc > 5) ? argv[5] : false; document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "") };
	Twi.Cookies.get = function (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; var j = 0; while (i < clen) { j = i + alen; if (document.cookie.substring(i, j) == arg) return Twi.Cookies.getCookieVal(j); i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break } return null };
	Twi.Cookies.clear = function (name) { if (Cookies.get(name)) { document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" } };
	Twi.Cookies.getCookieVal = function (offset) { var endstr = document.cookie.indexOf(";", offset); if (endstr == -1) { endstr = document.cookie.length } return unescape(document.cookie.substring(offset, endstr)) };

	/*当前登录用户*/
	Twi.CurrentUser = {
		FID: Twi.Cookies.get('Twi_User_FID'),
		FREALNAME: Twi.Cookies.get('Twi_User_FREALNAME'),
		FNAME: Twi.Cookies.get('Twi_User_FNAME'),
		PHONENUMBER: Twi.Cookies.get('Twi_User_PHONENUMBER'),
		PARKCODE: Twi.Cookies.get('Twi_User_PARKCODE'),
		STORAGECODE: Twi.Cookies.get('Twi_User_STORAGECODE')
	};

	Twi.ClearUserCookies = function () {
		Cookies.clear("Twi_User_FID");
		Cookies.clear("Twi_User_FREALNAME");
		// Cookies.clear("Twi_User_FNAME");
		Twi.CurrentUser.FID = "";
		Twi.CurrentUser.FREALNAME = "";
		Twi.CurrentUser.FNAME = "";
		Twi.CurrentUser.PHONENUMBER = "";
		Twi.CurrentUser.PARKCODE = "";
		Twi.CurrentUser.STORAGECODE = "";
	}

	/**
	 * 初始化方法
	 */
	$(function () {
		if (twiSelf) {
			if (twiVars && twiVars.autoInit == false) {
				//twiVars.autoInit = false  取消自动调用twiSelf.init();
			}
			else {
				if (typeof twiSelf.init == "function") twiSelf.init();
			}
		}		
	});
	

})(window.jQuery);


